package peng.yang.express.Fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import peng.yang.express.Activity.CheckActivity;
import peng.yang.express.Activity.ExpNameActivity;
import peng.yang.express.Activity.MainActivity;
import peng.yang.express.Adapter.LeftListViewAdapter;
import peng.yang.express.Bean.ExpressMember;
import peng.yang.express.Bean.PhoneMembers;
import peng.yang.express.R;
import peng.yang.express.Util.PhoneMembersDao;
import peng.yang.express.zxing.android.CaptureActivity;

/**
 * Created by Administrator on 2016/6/24.
 */
public class Fragment01 extends Fragment implements View.OnClickListener {

    private static final int REQUEST_CODE_SCAN = 1;
    private static final int REQUEST_NAME = 2;
    private static final String DECODED_CONTENT_KEY = "codedContent";
    final String TAG = "Fragment01";
    private View rootView;
    private EditText et_dimensioncode;
    private ImageButton bt_camera;
    private TextView et_company;
    private ImageButton bt_name;
    private Button bt_submit;
    String com = "";
    String nu = "";
    private EditText et_goodName;
    private ExpressMember member;
    private String url = "http://route.showapi.com/64-19?showapi_appid=20075&showapi_sign=a40bc8ce822d42b585b28d5b39cb9953";
    private String goodName;
    private DrawerLayout leftDrawer;
    private ListView listView;
    private PhoneMembersDao dao;
    private List<PhoneMembers> list;
    private ImageButton bt_clearnu;
    private ImageButton bt_clearcom;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        dao = new PhoneMembersDao(getContext());
        list = dao.getAllMembers();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_01, null);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        initView(rootView);
        return rootView;
    }


    private void initView(View rootView) {
        et_dimensioncode = (EditText) rootView.findViewById(R.id.et_dimensioncode);
        bt_camera = (ImageButton) rootView.findViewById(R.id.bt_camera);
        et_company = (TextView) rootView.findViewById(R.id.et_company);
        bt_name = (ImageButton) rootView.findViewById(R.id.bt_name);
        bt_submit = (Button) rootView.findViewById(R.id.bt_submit);
        bt_clearnu = (ImageButton) rootView.findViewById(R.id.bt_clearnu);
        bt_clearcom = (ImageButton) rootView.findViewById(R.id.bt_clearcom);
        et_goodName = (EditText) rootView.findViewById(R.id.et_goodName);


        Drawable drawable01 = getResources().getDrawable(R.drawable.shizixian);
        drawable01.setBounds(0, 0, 40, 40);
        et_goodName.setCompoundDrawables(drawable01, null, null, null);

        Drawable drawable02 = getResources().getDrawable(R.drawable.scan);
        drawable02.setBounds(0, 0, 50, 40);
        et_dimensioncode.setCompoundDrawables(drawable02, null, null, null);

        Drawable drawable03 = getResources().getDrawable(R.drawable.bus);
        drawable03.setBounds(0, 0, 50, 40);
        et_company.setCompoundDrawables(drawable03, null, null, null);


        leftDrawer = (DrawerLayout) rootView.findViewById(R.id.left_drawer);
        listView = (ListView) rootView.findViewById(R.id.left_listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv_expName = (TextView) view.findViewById(R.id.tv_expName);
                String expName = tv_expName.getText().toString();
                if(!expName.isEmpty()){
                    PhoneMembers bean = dao.queryExpressexpName(expName);
                    com = bean.getSimpleName().toString();
                    et_company.setText(expName);
                    leftDrawer.closeDrawer(Gravity.RIGHT);
                }

            }
        });


        LeftListViewAdapter listAdapter = new LeftListViewAdapter(getContext(), list);
        listView.setAdapter(listAdapter);

        bt_camera.setOnClickListener(this);
        bt_name.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        et_dimensioncode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                bt_clearnu.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               /* if(s.subSequence(start,before).toString().contains("")){
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);
                    builder.setMessage("你输出的数字包含空格，从新输入");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });

                    dialog = builder.create();
                    dialog.show();
                }*/
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!"".equals(s)){
                    bt_clearnu.setVisibility(View.VISIBLE);
                }else {
                    bt_clearnu.setVisibility(View.INVISIBLE);
                }
            }
        });

    et_company.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            bt_clearcom.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!"".equals(s)){
                bt_clearcom.setVisibility(View.VISIBLE);
            }else {
                bt_clearcom.setVisibility(View.INVISIBLE);
            }

        }
    });

        bt_clearnu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_dimensioncode.setText("");
                bt_clearnu.setVisibility(View.INVISIBLE);
            }
        });
        bt_clearcom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_company.setText("");
                bt_clearcom.setVisibility(View.INVISIBLE);
            }
        });


        et_company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftDrawer.openDrawer(Gravity.RIGHT);

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.bt_camera:
                Intent intent2 = new Intent(getContext(),
                        CaptureActivity.class);
                startActivityForResult(intent2, REQUEST_CODE_SCAN);

                break;
            case R.id.bt_name:
                Intent intent3= new Intent(getActivity(), ExpNameActivity.class);
                startActivityForResult(intent3,REQUEST_NAME);
                break;
            case R.id.bt_submit:
                nu = et_dimensioncode.getText().toString();
                if (et_goodName.getText().toString().trim().equals("")) {
                    goodName = et_goodName.getText().toString();
                }
                if(!"".equals(et_company.getText().toString().trim())&&!"".equals(nu.trim())){
                    Intent intent = new Intent(getContext(), CheckActivity.class);
                    intent.putExtra("com", com);
                    intent.putExtra("nu", nu);
                    intent.putExtra("gondName", goodName);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQUEST_CODE_SCAN:
            if (data != null&&requestCode==-1) {
                String content = data.getStringExtra(DECODED_CONTENT_KEY);
                et_dimensioncode.setText( content);
                break ;

            }
            case REQUEST_NAME:
                if (data != null) {
                    com = data.getStringExtra("sigleName");
                    String expName = data.getStringExtra("expName");
                    Log.e(TAG,com);
                    et_company.setText( expName);
                    break ;
                }
        }
    }
}
